﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.IO;
using GemBox.ExcelLite;
using System.Data;

namespace iMall.AtoZ.Helper
{
    public class TemplateEntity
    {
        public static string Path = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["TemplatePath"]);
        public static List<string> TemplateNameList;
        private static Dictionary<string, Dictionary<string, string>> _tmplateList;

        static TemplateEntity ( )
        {
            InitList();
        }

        public static Dictionary<string, Dictionary<string, string>> TemplateList
        {
            get
            {
                if ( _tmplateList == null )
                {
                    _tmplateList = new Dictionary<string, Dictionary<string, string>>();
                }

                if ( _tmplateList.Count < 1 )
                {
                    InitList();
                }

                return _tmplateList;
            }
        }

        public static void InitList ( )
        {
            if ( TemplateNameList != null )
            {
                TemplateNameList.Clear();
            }
            else
            {
                TemplateNameList = new List<string>();
            }

            if ( _tmplateList == null )
            {
                _tmplateList = new Dictionary<string, Dictionary<string, string>>();
            }
            else
            {
                _tmplateList.Clear();
            }

            if ( File.Exists(Path) )
            {
                List<string> tables = GemBoxExcelLiteHelper.GetExcelSheetNames(Path);

                foreach ( string tbName in tables )
                {
                    DataSet ds = GemBoxExcelLiteHelper.LoadExcel(Path, tbName, HDRString.YES, tbName);
                    if ( ds.Tables[tbName].Rows.Count < 1 || ds.Tables[tbName].Columns.Count < 2 )
                    {
                        continue;
                    }
                    TemplateNameList.Add(tbName);
                    _tmplateList[tbName] = new Dictionary<string, string>();
                    foreach ( DataColumn dc in ds.Tables[tbName].Columns )
                    {
                        _tmplateList[tbName][dc.ColumnName] = ds.Tables[tbName].Rows[0][dc.ColumnName].ToString();
                    }
                }
            }
        }

        public static Dictionary<string, string> GetTemplate ( DataRow header, string merchant )
        {
            int headerCount = header.ItemArray.Count<object>();
            Dictionary<string, string> re = new Dictionary<string, string>();
            Dictionary<string, int> dictRe = new Dictionary<string, int>();

            foreach ( KeyValuePair<string, Dictionary<string, string>> item in TemplateList )
            {
                int count = 0;
                if ( item.Key.IndexOf(merchant) < 0 )
                {
                    continue;
                }

                foreach ( var value in header.ItemArray )
                {
                    if ( value == null || value.ToString().Length < 1 )
                    {
                        continue;
                    }
                    else if ( item.Value.ContainsKey(value.ToString().Trim()) )
                    {
                        count++;
                    }
                }

                dictRe[item.Key] = count;
            }

            int hold = 0;
            int offset = 0;
            foreach ( KeyValuePair<string, int> item in dictRe )
            {
                if ( ( item.Value > hold && item.Value <= headerCount ) || ( item.Value == hold && offset > ( TemplateList[item.Key].Count - item.Value ) ) )
                {
                    offset = TemplateList[item.Key].Count - item.Value;
                    hold = item.Value;
                    re = TemplateList[item.Key];
                }
            }

            return re;
        }
    }
}